package tv.youxing.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.adapter.LiShiPlayerAdapter;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.LiShiPlayerModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

import static com.mob.MobSDK.getContext;


/**
 * 历史播放记录
 */
public class LiShiPlayerActivity extends BaseActivity {

    @BindView(R.id.lv)
    PullToRefreshListView lv;
    LiShiPlayerModel allDatas;
    List<LiShiPlayerModel.ResultBean.DataBean> mListDatas = new ArrayList<>();

    int page = 1;
    private LiShiPlayerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_li_shi_player);
        ButterKnife.bind(this);


        if (UtilTools.LoginJudge()) {
            initView();
            requestData();
        } else {
            CustomToast.INSTANCE.showToast("请先登陆");
        }




    }

    private void initView() {
        mAdapter = new LiShiPlayerAdapter(this, mListDatas);
        lv.setMode(PullToRefreshBase.Mode.BOTH);
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                mListDatas.clear();
                requestData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                requestData();
            }
        });
        lv.setAdapter(mAdapter);
    }

    private void requestData() {
        //post请求简洁版实现
        HttpParams params = new HttpParams();
        params.put("page", page);
        params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(getContext(), AppConst.TOKEN, ""));

        RxVolley.post(StaticClass.LiShiPlayer, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                allDatas = GsonUtils.GsonToBean(t, LiShiPlayerModel.class);

                if (allDatas.getCode() == AppConst.TOKENERROR) {
                    LiShiPlayerActivity.this.startActivity(new Intent(LiShiPlayerActivity.this, LoginActivity.class));
                    return;
                }

                if (allDatas.getCode() == 200) {
                    if (allDatas.getResult().getData().size() > 0) {
                        mListDatas.addAll(allDatas.getResult().getData());
                        mAdapter.notifyDataSetChanged();
                    } else {
                        CustomToast.INSTANCE.showToast("没数据了");
                    }
                }


                if (lv.isRefreshing()) {
                    lv.onRefreshComplete();
                }
            }
        });
    }
}
